package com.yu.rabbitmq.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//消费者
public class Consumer {
    private static String QUEUE_NAME = "hello";

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("121.40.205.58");//如果连接不上，记得开5672的端口
        factory.setUsername("admin");
        factory.setPassword("123456");
        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            DeliverCallback callback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody());
                System.out.println(message);
            };

            CancelCallback cancelCallback = (consumerTag)->{
                System.out.println("消息消息被中断");
            };
            /**
             * 消费者消费消息
             * 1.消费哪个队列
             * 2.消费成功之后是否要自动应答 true 代表自动应答 false 手动应答
             * 3.消息的回调（是个接口，所以我们用landom表达式来实现）
             * 4.消息失败时候的回调（是个接口，所以我们用landom表达式来实现）
             *
             */
            channel.basicConsume(QUEUE_NAME,true,callback,cancelCallback);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}
